#ifndef MTV_SERVICE_H_INCL
#define MTV_SERVICE_H_INCL
#if __GNUC__ > 3 || _MSC_VER > 1020
#pragma once
#endif

#ifndef __cplusplus
#error this header requires C++ compiler
#endif
/*******************************************************************************
Copyright (C), 2008, Borqs Tech. Co., Ltd.
File Name:
mtv_service.h
Description:
Provide MobileTV related APIs for upper layer applications
1. ServiceManager for service&conent related synchronous APIs
2. ServiceProc for select, updateSG, GBA, subscirbe, unsubscribe, authorize,
   inquiry, initialize related asynchronous APIs
History:
Date          Author                         Comments
-----------   ---------------------------    --------------------
05-Jan-2008   zhenyu.wu@borqs.com            Created
20-Mar-2008   jinyun.zhang@borqs.com         Updated the API Interface
20-Jun-2008   song.bi@borqs.com              Updated the API Interface
01-Sep-2008   song.bi@borqs.com              Updated the API Interface
21-May-2009   song.bi@borqs.com              Updated the API Interface
*******************************************************************************/

#include <time.h>

#include "mtvservice_event.h"
#include "service_def.h"



class  MTV_Service
{
public:

    // ----------------------------------------------------------------------------------------
    // Inner type definition.
    // ----------------------------------------------------------------------------------------

    // Preference is used to control the inner running mechanism.
    struct Preference
    {
        enum
        {
            TEL_ENV_UNKNOWN = 0,    // Unknown environment.
            TEL_ENV_GSM = 1,        // GSM environment.
            TEL_ENV_3G = 2          // 3G environment.
        };

        // Telephone type.
        int telEnvType;

        // Adventure.
        // In some situations, PurchaseChannel needs to be specified.
        const char *purchaseChannelId;

        // The interval of change of stream key.
        unsigned int streamKeyInterval;

        // GPRS network connection setting.
        const char *apn;        // APN
        const char *wapGateway; // WAP Gateway Domain
        int wapPort;            // WAP Gateway Port

        // Setting for MBBMS procedures.
        const char *nafid;      // NAF Server ID
        const char *nafDomain;  // NAF Server Domain
        const char *sgDomain;   // SG Server Domain

        int nafPort;            // NAF Server Port
        int sgPort;             // SG Server Port
    };

    enum ACTIVATE_TYPE
    {
        REGISTER_ACTIVATION = 0,
        CANCELLATION_ACTIVATION = 1,
        PAUSE_ACTIVATION = 2,
        RESUME_ACTIVATION = 3,
        CHANGE_USER_PROFILE_ACTIVATION = 4
    };

public:
    // ----------------------------------------------------------------------------------------
    // API Functions related with general function.
    // ----------------------------------------------------------------------------------------

    /*
     * Set lisenter for the MTV Service.
     * @param listener - the listenser of MTV service for callback usage.
     * \After a process finishes, the listener will be called.
     * @param cookie - the data passed to the listener.
     * \note - Synchronous function, only support 1 listener
     */
    virtual void setListener(service_listener_f listener, void *cookie =
                                 NULL) = 0;

    /*
     * Used to set preference of MTV Service.
     * @param preference - the object of Preference.
     * @return int - the result of success or failure.
     * \0, successfully.
     * \-1, failed.
     * \note - Synchronous function
     * \The function should be called once the settings are changed every time.
     */
    virtual int setPreference(Preference const &preference) = 0;

    // ----------------------------------------------------------------------------------------
    // API Functions related with SGDD.
    // ----------------------------------------------------------------------------------------
    /*
     * Get SGDD in local Service Guide
     * @return SGDDBrief* - a pointer to the SGDDBrief
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     */
    virtual SGDDBrief *getSGDDBrief() = 0;


    //virtual char * getServiceManager(const char *serviceID) = 0;

    // ----------------------------------------------------------------------------------------
    // API Functions related with ServiceGuide.
    // ----------------------------------------------------------------------------------------
    /*
     * Get all Service in local Service Guide
     * @return ServiceBrief* - a pointer to all ServiceBriefs
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     */
    virtual ServiceBrief *getAllServiceBrief() = 0;

    /*
     * Get a complete Service.
     * @param serviceID - a Service ID
     * @return Service* - a pointer to a Service
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function is usually used to get detail information of a Service.
     */
    virtual MBBMS_Service * getService(const char *serviceID) = 0;

    /*
     * Get Access information of CMMB of a Service
     * @param serviceID - a Service id
     * @return char* - a formatted string of access information
     * \the format of access information:
     * \mbbms://cmmb?freqdot=&serviceid=&CAS_ID=&protected=&sdptxt=
     * \Parameters:
     * \freqdot, serial number of frequency dot.
     * \serviceid, service id of CMMB in a frequency dot.
     * \CAS_ID, ID of BSMSelector in SGDD.
     * \protected, flag of protected or not.
     * \sdptxt, content of a SDP.
     * \note - Synchronous function
     * \The string returned need to be freed.
     */
    virtual char *getCMMBAccess(const char *serviceID) = 0;

    /*
     * Get all brief Content of a Service
     * @param serviceID - the service id
     * @return ContentBrief* - a pointer to several ContentBriefs.
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     */
    virtual ContentBrief *getAllContentBrief(const char *serviceID) = 0;

    /*
     * Search all Content in terms of Keywords, Service ID, time interval, Genre.
     * @param key - Keywords
     * @param serviceId - a Service ID
     * @param from - start time in a time interval
     * @param to - end time in a time interval
     * @param genre - the type of Content
     * @return Content* - a pointer to several BriefConents
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \"key" must not be null, others may be null.
     * \The values of "from" and "to" are as same as time_t.
     * \If "from" or "to" is 0, the condition of "from" or "to" will be ignored.
     */
    virtual ContentBrief *searchContent(const char *key,
                                        const char *serviceId,
                                        unsigned long from,
                                        unsigned long to,
                                        const char *genre) = 0;

    /*
     * Get a complete Content.
     * @param contentID - a Content ID
     * @return Content* - a pointer to a Content
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function is usually used to get detail information of a Content.
     */
    virtual MBBMS_Content * getContent(const char *contentID) = 0;

    /*
     * Get all PurchaseChannels
     * @return PurchaseChannel* - a pointer to several PurchaseChannels.
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     */
    virtual MBBMS_PurchaseChannel * getAllPurchaseChannel() = 0;

    /*
     * Get all PurchaseItems by Service category
     * @return PurchaseItemBrief* - a pointer to several PurchaseItem.
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function is often used for subscription of a channel in a month.
     */
    virtual PurchaseItemBrief *getPurchaseItemByService() = 0;

    /*
     * Get all PurchaseItems of Contents which belongs to a Service.
     * @param serviceID - a Service id
     * @return PurchaseItemBrief* - a pointer to several PurchaseItems
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function is often used for subscription of a program only in a short time interval.
     */
    virtual PurchaseItemBrief *getPurchaseItemByContent(const char
            *serviceID) = 0;

    /*
     * Get PurchaseItem by bundle category.
     * @return PurchaseItemBrief* - a pointer to several PurchaseItems.
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function is often used for subscription of several channels or programs together.
     */
    virtual PurchaseItemBrief *getPurchaseItemByBundle() = 0;

    /*
     * Get all PurchaseItems related to a Service.
     * @param id - a Service ID
     * @return PurchaseItemBrief* - head pointer of several PurchaseItemBriefs
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     */
    virtual PurchaseItemBrief *getPurchaseItemOfService(const char *id) =
        0;

    /*
     * Get all PurchaseItems related to a Content.
     * @param id - a Content ID
     * @return PurchaseItemBrief* - head pointer of several PurchaseItemBriefs
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     */
    virtual PurchaseItemBrief *getPurchaseItemOfContent(const char *id) =
        0;

    /*
     * Get all sub-PurchaseItems related to a PurchaseItem.
     * @param id - a PurchaseItem ID
     * @return PurchaseItemBrief* - head pointer of several PurchaseItemBriefs
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function is used for get what is in a PurchaseItem, especially those of a bundle type.
     */
    virtual PurchaseItemBrief *getSubPurchaseItem(const char
            *purchaseItemID) = 0;

    /*
     * Search all PurchaseItems in terms of Keywords, Service ID.
     * @param key - keywords
     * @param serviceID - a Service ID
     * @return PurchaseItemBrief* - head pointer of several PurchaseItemBriefs
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \"key" must not be null, others may be null.
     */
    virtual PurchaseItemBrief *searchPurchaseItem(const char *key,
            const char *serviceID) =
                0;

    /*
     * Update the latest Service Guide from server to local.
     * @return int - always return 0
     * \note - Asynchronous function
     */
    virtual int startUpdateServiceGuide() = 0;

    /*
     * Todo - Stop while updating Service Guide.
     * @return int - always return 0
     * \note - Asynchronous function
     */
    virtual int stopUpdateServiceGuide() = 0;

    // ----------------------------------------------------------------------------------------
    // API Functions related with transaction.
    // ----------------------------------------------------------------------------------------

    /*
     * Execute inquiry process to update my subscription information
     * @return int - always return 0
     * \note - Asynchronous function
     * \After it finishes, call "getAllSubscriptions" to get my subscription.
     */
    virtual int inquiry() = 0;

    /*
     * Get all PurchaseItems of subscribed Service and Content in local.
     * @return PurchaseItemBrief* - a pointer to several PurchaseItemBriefs;
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function should be called after "inquiry" is finished.
     */
    virtual PurchaseItemBrief *getAllSubscriptions() = 0;

    /*
     * Execute subscription process to subscribe several PurchaseItems.
     * @param purchaseID - a pointer of several globalPurchaseitemID
     * @param length - the length of the array.
     * @return int - always return 0
     * \note - Asynchronous function
     */
    virtual int subscribe(const char *const purchaseID[], int length) = 0;

    /*
     * Execute unsubscribe process to unsubscribe several PurchaseItems.
     * @param purchaseID - a pointer of several globalPurchaseitemID
     * @param length - the length of the array.
     * @return int - always return 0
     * \note - Asynchronous function
     */
    virtual int unsubscribe(const char *const purchaseID[], int length) =
        0;

    /*
     * Execute unsubscribe process to unsubscribe all PurchaseItems subscribed.
     * @return int - always return 0
     * \note - Asynchronous function
     */
    virtual int unsubscribeAll() = 0;

    /*
     * Update the latest subscription information table from server to local
     * @return int - always return 0
     * \note - Asynchronous function
     * \The function should be called when subscription is finished or a new startup.
     */
    virtual int updateSubscriptionTable() = 0;

    // ----------------------------------------------------------------------------------------
    // API Functions related with security.
    // ----------------------------------------------------------------------------------------

    /*
     * Execute GBA bootstrap process
     * @param force - a flag if needs to force to start an GBA procedure.
     * \Default it is not.
     * @return int - always return 0
     * \note - Asynchronous function
     * \The function should be called when a new startup.
     */
    virtual int initialize(int force = 0) = 0;

    /*
     * Execute activation process
     * @param type - the type of activation.
     * \REGISTER_ACTIVATION, register MTV service.
     * \CANCELLATION_ACTIVATION, cancel MTV service.
     * \PAUSE_ACTIVATION, pause MTV service.
     * \RESUME_ACTIVATION, resume MTV service.
     * \CHANGE_USER_PROFILE_ACTIVATION, change the user profile for MTV service.
     * @return int - always return 0
     * \note - Asynchronous function
     */
    virtual int activate(ACTIVATE_TYPE type) = 0;

    /*
     * Execute authorization process
     * @param sms - a string of a SMS of WAP PUSH.
     * @return int - always return 0
     * \note - Asynchronous function
     * \The function is used when a WAP push SMS is coming.
     */
    virtual int authorize(const char *sms) = 0;

    /*
     * Execute authorization process of A Service
     * @param serviceID - Service ID.
     * @return int - result of authorization.
     * \0, executing authorization process;
     * \1, authorization process has been already finished.
     * \note - Asynchronous function
     * \The function is used before trying to play a Service,
     * \if it returns 0, the player needs to wait until the service key is retrieved.
     */
    virtual int authorizeService(const char *serviceID) = 0;

    // ----------------------------------------------------------------------------------------
    // API Functions related with interactivity.
    // ----------------------------------------------------------------------------------------

    /*
     * Execute interactivity process
     * @param serviceID - A Service ID
     * @return int - always return 0
     * \note - Asynchronous function
     * \The function is often used when playing.
     * \After it finishes, call "getInteractivity" to get interactivities.
     */
    virtual int interact(const char *serviceID) = 0;

    /*
     * Get interactivity object after interactivity process in local
     * @return MediaObjectGroup* - a pointer to a MediaObjectGroup
     * \note - Synchronous function
     * \The pointer returned need to be freed.
     * \The function needs to be called after "interact" finished.
     */
    virtual MBBMS_MediaObjectGroup * getInteractivity() = 0;

    MTV_Service()
    {
    };

    virtual ~ MTV_Service()
    {
    };
};

/*
 * Increase reference count of the instance of MTV Service
 * @return MTV_Service* - a pointer to a MTV_Service
 * \note - Synchronous function
 * \Only an instance exists, in spite of the function can be called repeatedly.

EXTERN*/
MTV_Service *MTV_createService();

/*
 * Decrease reference count of the instance of MTV Service
 * @param MTV_Service*& - a pointer to a MTV_Service
 * \note - Synchronous function
 * If the reference count is 0, the instance will be released.

EXTERN*/
void MTV_destroyService(MTV_Service * &pMTVService);

/*
 * Set MBBMS Protocol Data Path
 * @param path - path to store MBBMS protocol datas
 * @param image_path - path to store MBBMS protocol pictures which may be equal with path.
 * @return void
 * \note - Synchronous function
 * This function should be called at the beginning, and only once.
 */

extern "C" void set_mbbms_path(const char *path, const char *image_path);

#endif                          // end of #ifndef MTV_SERVICE_H_INCL
